import {extend} from 'umi-request'
import {message} from "antd";
import {history} from "umi";
import {stringify} from "querystring";

const request = extend({
  credentials: 'include',
  prefix: process.env.NODE_ENV === 'production' ? '123.56.100.47:8080' : undefined
})

request.interceptors.response.use(async (response, options): Promise<any> => {
  const res = await response.clone().json();
  if (res.code === 0) {
    return res.data
  }
  //如果报错未登录，则直接跳转到登录页面，并记录当前页面的路由
  if (res.code === 40101) {
    message.error("请先登录")
    history.replace({
      pathname: '/user/login',
      search: stringify({
        redirect: location.pathname
      })
    })

  } else {
    message.error(res.description)
  }

});

export default request;
